using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using physics = Microsoft.Robotics.Simulation.Physics;

namespace ProMRDS.Simulation.JointMover
{
    public partial class SimulatedBipedMoverUI : Form
    {
        FromWinformEvents _fromWinformPort;

        public SimulatedBipedMoverUI(FromWinformEvents EventsPort)
        {
            _fromWinformPort = EventsPort;

            InitializeComponent();

            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.Loaded, null, this));
        }

        public void AddEntityName(string name)
        {
            _entityName.Text = name;
        }

        public void ClearSliders()
        {
            this.splitContainer1.Panel2.Controls.Clear();
            this.splitContainer1.Panel2.Invalidate();
        }

        public void AddSliders(Dictionary<string,DOFDesc> joints)
        {
            Stack<Control> tmp = new Stack<Control>();

            const int spacing = -18;
            const int labelWidth = 80;
            int y = 5;
            int x = 5;
            foreach(DOFDesc desc in joints.Values)
            {
                Label lb = new Label();
                lb.Location = new System.Drawing.Point(x, y);
                lb.Size = new System.Drawing.Size(labelWidth, 45);
                lb.Text = desc.Name;

                tmp.Push(lb);

                TrackBar tb = new TrackBar();
                tb.Location = new System.Drawing.Point(x + labelWidth, y);

                tb.Maximum = (int)(desc.Maximum * desc.Scale);
                tb.Minimum = (int)(desc.Minimum * desc.Scale);
                tb.Name = desc.Name;
                tb.Size = new System.Drawing.Size(432 - labelWidth, 45);
                tb.TabIndex = 0;
                tb.TickFrequency = 3;
                tb.TickStyle = TickStyle.None;
                tb.Scroll += new System.EventHandler(this.trackBar_Scroll);
                
                tmp.Push(tb);

                Label vlb = new Label();
                vlb.AutoSize = true;
                vlb.Location = new System.Drawing.Point(x + labelWidth + tb.Width + 3, y);
                vlb.Text = "0";
                tb.Tag = new object[] { desc, vlb };
                tmp.Push(vlb);
                y += tb.Size.Height + spacing;
            }

            // controls are added in reverse order because they overlap
            while(tmp.Count > 0)
                this.splitContainer1.Panel2.Controls.Add(tmp.Pop());

            this.splitContainer1.Panel2.Invalidate();
        }

        private void trackBar_Scroll(object sender, EventArgs e)
        {
            TrackBar tb = (TrackBar)sender;
            DOFDesc  desc = (DOFDesc)((object[])tb.Tag)[0];
            Label lb = (Label)((object[])tb.Tag)[1];
            lb.Text = (tb.Value/desc.Scale).ToString();
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.MoveJoint, null, new MoveJoint(tb.Value / desc.Scale, desc.Name)));
        }

        private void _suspendBox_CheckedChanged(object sender, EventArgs e)
        {
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.Suspend, null, _suspendBox.Checked));
        }

        private void _changeEntityButton_Click(object sender, EventArgs e)
        {
            _fromWinformPort.Post(new FromWinformMsg(FromWinformMsg.MsgEnum.ChangeEntity, null, _entityName.Text));
        }
    }
}